import React, { Component } from 'react'
import axios from 'axios'
import './index.css'

export default class Search extends Component {
  search = () => {
    // 获取用户的输入
    // 连续结构赋值 + 重命名
    const {
      keyWordElement: { value: keyword }
    } = this

    console.log(keyword)
    // 发送请求前通知App更新状态
    this.props.updateAppState({
      isFirst: false,
      isLoading: true
    })
    // 发送网络请求
    axios.get(`https://api.github.com/search/users?q=${keyword}`).then(
      (response) => {
        console.log('成功了', response)
        this.props.updateAppState({
          users: response.data.items,
          isLoading: false
        })
      },
      (error) => {
        this.props.updateAppState({
          isLoading: false,
          err: error.message
        })
        console.error('失败了', error)
      }
    )
  }
  render() {
    return (
      <div>
        <section className="jumbotron">
          <h3 className="jumbotron-heading">搜索github用户</h3>
          <div>
            <input
              ref={(c) => (this.keyWordElement = c)}
              type="text"
              placeholder="输入关键词点击搜索"
            />
            &nbsp;<button onClick={this.search}>搜索</button>
          </div>
        </section>
      </div>
    )
  }
}
